/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');


// Schema 结构
var TgMapPcModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
  //`pc_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'id',
  //`map_id` bigint(20) unsigned NOT NULL COMMENT '文档id',
  //`page_id` bigint(20) unsigned NOT NULL COMMENT '页ID',
  //`pc_index` int(3) NOT NULL DEFAULT '0' COMMENT '下标（0开始）',
  //`backup_flg` int(1) NOT NULL DEFAULT '0' COMMENT '0普通页; 1保留页',
  //`data` mediumtext COMMENT '页内容',
  //`replaces` mediumtext COMMENT '可替换组',
  //`hash_tag` varchar(100) DEFAULT '0' COMMENT '标记',
  //`thumb_s` text COMMENT '缩略图小',
  //`thumb_m` text COMMENT '缩略图中',
  //`thumb_l` text COMMENT '缩略图大',
  //`audio` text COMMENT '音频url数组',
  //`video` text COMMENT '视频url数组',
  //`media` text COMMENT '原始视频',
  //`dirty` int(1) NOT NULL DEFAULT '0' COMMENT '0未修改; 1页被修改过',
  //`create_time` datetime NOT NULL,
  //`update_time` datetime NOT NULL,
  //`update_by` varchar(50) NOT NULL DEFAULT '',
  //`del_flg` int(1) NOT NULL DEFAULT '0',

};
TgMapPcModel.prototype = new ItbBaseModel();

// 取得表名
TgMapPcModel.prototype.getTableName = function(){
    return 'tg_map_pc';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
TgMapPcModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'pc_id='+this.pc_id;
}

//------------------------------------------------------------------
/* 获取分页pc信息
 * 参数:map_id/page_id/pc_id
 * 参数:callback:回调
 * */
TgMapPcModel.prototype.getMapPcInfoById = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' select t.pc_id, t.page_id, t.map_id, t.pc_index, t.backup_flg, '+
            ' t.data, t.replaces, t.hash_tag, t.thumb_s, t.dirty '+
            ' from TG_MAP_PC t'+
            ' where t.del_flg = 0 ';
        if(THIS.map_id){
            sql += ' and t.map_id = '+ THIS.map_id;
        }
        if(THIS.page_id){
            sql += ' and t.page_id = '+ THIS.page_id;
        }
        if(THIS.pc_id){
            sql += ' and t.pc_id = '+ THIS.pc_id;
        }
        sql += ' order by t.map_id, t.page_id, t.pc_index ';

        //var sqlValue = [THIS.type_id];
        //sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            //console.log();

            callback(error, result);
        });
    });
}
//--------------------------------end-------------------------------

//------------------------------------------------------------------
/* 获取分页信息(按page为单位)
 * 参数:map_id/page_id
 * 参数:callback:回调
 * */
TgMapPcModel.prototype.getMapPageAndPcInfo = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        // Use the connection
        var sql = ' select t.pc_id, t.pc_index, t.page_id, p.page_index, t.map_id, '+
            ' p.backup_flg AS page_backup_flg, t.data AS pc_data, p.data AS page_data '+
            ' from TG_MAP_PAGE p, TG_MAP_PC t '+
            ' where p.del_flg = 0 and t.del_flg = 0 '+
            ' and p.page_id = t.page_id and p.map_id = t.map_id ';
        if(THIS.map_id){
            sql += ' and t.map_id = '+ THIS.map_id;
        }
        if(THIS.page_id){
            sql += ' and t.page_id = '+ THIS.page_id;
        }
        sql += ' order by t.map_id, p.page_index, t.pc_index ';

        //var sqlValue = [THIS.type_id];
        //sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();
            //console.log();


            if(result && result.length>0) {
                var retInfo=[];

                var tmpArrPc=new Array();
                var tmpPage = result[0];

                for(var i in result) {
                    var item = result[i];

                    if(item.page_id != tmpPage.page_id) {
                        retInfo.push({
                            page_id: tmpPage.page_id,
                            page_index: tmpPage.page_index,
                            data: tmpPage.page_data,
                            pcData: tmpArrPc
                        });

                        tmpPage = result[i];
                        tmpArrPc = new Array();
                    }

                    tmpArrPc.push({
                        pc_id: result[i].pc_id,
                        pc_index: result[i].pc_index,
                        data: result[i].pc_data
                    });

                    if(i==result.length-1) {// 最后一条
                        retInfo.push({
                            page_id: tmpPage.page_id,
                            page_index: tmpPage.page_index,
                            data: tmpPage.page_data,
                            pcData: tmpArrPc
                        });
                    }
                }

                callback(error, retInfo);

            } else {
                callback(error, null);
            }
        });
    });
}
//--------------------------------end-------------------------------


// 导出
module.exports = TgMapPcModel;